package com.orange.lockscreen.layers;

import java.util.Locale;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.widget.RelativeLayout;

import com.orange.lockscreen.SlatHashMap;
import com.orange.lockscreen.common.LogHelper;

public abstract class GridLayout extends RelativeLayout {

	private static final String	TAG						= "GridLayout";
	protected int				mOldWidthMeasureSpec	= Integer.MAX_VALUE;
	protected int				mOldHeightMeasureSpec	= Integer.MAX_VALUE;

	public GridLayout(Context context) {
		super(context);
	}

	protected void layoutChild(View child, int rowIndex) {
//		LogHelper.v(TAG, "layoutChild. rowIndex=" + rowIndex);
		int childLeft = 0;
		int childTop = SlatHashMap.getSlatTop(rowIndex);
//		int childRight = getWidth();
		int childRight = SlatHashMap.getScreenWidth();
		int childBottom = childTop + SlatHashMap.getSlatHeight(rowIndex);
		/**
		 * add by gaoge 2011-11-25 for fix bug that when landscape,the lock png dispear
		 */
		if(rowIndex == SlatHashMap.UNLOCK_ROWINDEX){
			LogHelper.v("LockPar", "unlock;childLeft: " + childLeft + ",childTop: "
					+ childTop + ",childRight: " + childRight + ",childBottom: " + childBottom );
			LogHelper.v("LockPar", "^^^^^^^^^^^^^SlatHashMap.getScreenWidth(): " + SlatHashMap.getScreenWidth());
			String sBuildModel = Build.MODEL.toLowerCase(Locale.UK);
			
			if (sBuildModel.contains("acer")){
				LogHelper.d("LockPar","480 == childRight: " + (480 == childRight));
				if(480 == childRight) {
					childRight = 320;
					LogHelper.d("LockPar","$$$$$$$$$$$$$$$$$$$$$ change childRight to 320");
				}
			}
		}
		if (child != null) {
			child.layout(childLeft, childTop, childRight, childBottom);
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//		LogHelper.v(TAG, "onMeasure. ");
		int measureWidth = getMeasuredWidth();
		int measureHeight = getMeasuredHeight();
		if (measureWidth == 0 && measureHeight == 0) {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		} else {
			measureChildren(widthMeasureSpec, heightMeasureSpec);
			setMeasuredDimension(measureWidth, measureHeight);
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int count = getChildCount();
		
		if (LogHelper.enableDefaultLog) {
			LogHelper.v(TAG, "onLayout. who=" + this.getClass().getName()
					+ " changed=" + changed + ",child count: " + count);
		}
		
		for (int i = 0; i < count; i++) {
			View child = getChildAt(i);
			if (child.getVisibility() != GONE) {
				layoutChild(child, i);
			}
		}
	}

	public static class LayoutParams extends RelativeLayout.LayoutParams {
		public int	xOffset				= 0;
		public int	xHistoryOffset		= 0;
		public int	yOffset				= 0;
		public Rect	canTouchableRect	= null;

		public LayoutParams() {
			super(WRAP_CONTENT, WRAP_CONTENT);
		}

		public LayoutParams(int width, int height) {
			super(width, height);
		}
	}
}
